package com.gitee.linmt.advice;

import com.gitee.linmt.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 全局异常处理
 *
 * @author linmt
 */
@Slf4j
@ControllerAdvice
public class ShiroExceptionAdvice {

    /**
     * 权限认证失败的全局异常处理
     *
     * @param e 认证失败异常
     * @return 处理异常的路径
     */
    @ExceptionHandler(value = UnauthorizedException.class)
    public String unauthorizedExceptionHandler(UnauthorizedException e) {
        log.error("用户[{}]无权限[{}]操作",
                ((UserInfo)SecurityUtils.getSubject().getPrincipal()).getUsername(), e.getMessage(), e);
        return "/unauthorized";
    }
}
